package com.wj.rspmproperty.controller;

import com.wj.rspmproperty.pojo.*;
import com.wj.rspmproperty.service.BuildingService;
import com.wj.rspmproperty.service.FloorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author WeiJin
 * @version 1.0
 */
@Api(tags = "楼栋相关接口")
@RestController
@RequestMapping("/building")
@Transactional
public class BuildingController {

    @Autowired
    private BuildingService buildingService;
    @Autowired
    private FloorService floorService;


    /**
     * 查询在用建筑
     *
     * @return
     */
    @GetMapping("buildingOfUsed")
    @ApiOperation(value = "查询在用建筑列表", responseContainer = "List")
    public ResponsePojo<List<Building>> buildingOfUsed() {
        return ResponsePojo.success(buildingService.buildingOfUsed());
    }


    @PostMapping("/addBuilding")
    @ApiOperation(value = "新增单个建筑")
    public ResponsePojo<Integer> addBuilding(@RequestBody Building building) {
        int result = -1;
        try {
            result = buildingService.addBuilding(building);
            return ResponsePojo.success(result);
        } catch (DuplicateKeyException e) {
            return ResponsePojo.fail(result, "楼栋编号重复");
        }
    }

    /**
     * 分页查询楼栋
     *
     * @param current 当前页
     * @param size    每页多少条记录
     * @return
     */
    @ApiOperation(value = "查询楼栋分页信息", response = Building.class,
            responseContainer = "list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1",
                    dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页多少条记录", defaultValue = "10",
                    dataType = "Integer", paramType = "query")
    })
    @GetMapping("/page/building")
    public ResponsePojo<PageResult<Building>> pageOfBuilding(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return ResponsePojo.success(buildingService.pageOfBuilding(current, size));

    }

    @PostMapping("/addBuildings")
    @ApiOperation(value = "批量添加建筑", notes = "只给出一个楼栋的全属性，然后给出多少个楼栋，每层楼房键的建筑面积")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingSize", value = "剩余需添加的楼栋个数", required = true),
            @ApiImplicitParam(name = "builtArea", value = "每层楼房间的建筑面积，按房间序号排序")
    })
    public ResponsePojo<ResponseMap<String, Integer>> addBuildingsAndFloorsAndRooms(
            @RequestBody Building building,
            @RequestParam(value = "buildingSize", required = true) Integer buildingSize,
            @RequestParam(value = "builtArea", required = true) String builtArea) {

        Integer[] arr = Arrays.stream(builtArea.split(","))
                .map(Integer::parseInt).toArray(Integer[]::new);

        Integer buildingResult = 0;
        List<Building> buildings = new ArrayList<>(buildingSize + 1);
        Map<String, Integer> results = new HashMap<>();
        //添加楼栋
        try {
            buildingResult = buildingService.insertBuildings(building, buildings, buildingSize);
            results.put("builing", buildingResult);
        } catch (DuplicateKeyException e) {
            return ResponsePojo.fail(null, e.getMessage());
        }
        //初始化模板楼层信息
        results.put("floor", 0);
        results.put("room", 0);
        for (Building b : buildings) {
            Floor floor = new Floor();
            floor.setBelong_building(b.getBuilding_id());
            floor.setRoom_number((short) arr.length);
            floor.setBuilding_name(b.getBuilding_name());
            //每栋楼添加每一层
            floorService.insertFloors(floor, results, b.getBuilding_floors(), arr);
        }

        ResponseMap<String, Integer> res = new ResponseMap<>(results);
        return ResponsePojo.success(res);
    }

    /**
     * 批量修改楼栋建筑状态
     *
     * @param idList 需要修改的建筑id集
     * @param status 修改成什么状态
     * @return 返回修改的记录条数
     */
    @ApiOperation(value = "批量修改多个建筑的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList", required = true, value = "需要修改状态的楼栋id列表",
                    dataTypeClass = List.class, paramType = "query"),
            @ApiImplicitParam(name = "status", required = true, value = "修改后的状态")
    })
    @PutMapping("/updateStatus")
    public ResponsePojo<Integer> changeBuildingsStatus(
            @RequestParam("idList") List<Integer> idList,
            @RequestParam("status") Integer status) {

        if (idList.size() > 0) {
            try {
                return ResponsePojo.success(buildingService.changeBuildingsStatus(idList, status));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ResponsePojo.fail(null, "无数据");
    }
}
